<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="物料编码" prop="FMaterialIdfnumber">
        <el-input
          v-model="queryParams.FMaterialIdfnumber"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="FMaterialIdfname">
        <el-input
          v-model="queryParams.FMaterialIdfname"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          icon="el-icon-refresh"
          size="mini"
          v-hasPermi="['bom:base:getRemoteStap']"
          @click="dialogVisible = true"
          >更新物料</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['bom:unitTranfer:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['bom:unitTranfer:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['bom:unitTranfer:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['bom:unitTranfer:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="unitTranferList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column
        label="物料编码"
        align="center"
        prop="fmaterialIdfnumber"
      />
      <el-table-column
        label="物料名称"
        align="center"
        prop="fmaterialIdfname"
      />
      <el-table-column
        label="采购计量"
        align="center"
        prop="fconvertDenominator"
      />
      <el-table-column
        label="采购单位名称"
        align="center"
        prop="fcurrentUnitIdFName"
      />
      <el-table-column
        label="采购单位编码"
        align="center"
        prop="fcurrentUnitIdFNumber"
      />
      <el-table-column
        label="核算计量"
        align="center"
        prop="fconvertNumerator"
      />
      <el-table-column
        label="核算计量单位编码"
        align="center"
        prop="fdestUnitIdfnumber"
      />
      <el-table-column
        label="核算计量单位名称"
        align="center"
        prop="fdestUnitIdfname"
      />
      <el-table-column
        label="规格型号"
        align="center"
        prop="fmaterialIdFspecification"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="删除标志" align="center" prop="del">
        <template slot-scope="scope">
          <div v-if="scope.row.del == 0">正常</div>
          <div v-if="scope.row.del == 1">删除</div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['bom:unitTranfer:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['bom:unitTranfer:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料单位转化表对话框 -->
    <el-dialog
      @open="onMaOpen"
      @close="onMaClose"
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料编码" prop="fmaterialIdfnumber">
          <el-input
            v-model="form.fmaterialIdfnumber"
            @focus="materialsVisible = true"
            :disabled="editTitle"
            placeholder="请输入物料编码"
          />
        </el-form-item>
        <el-form-item label="物料名称" prop="fmaterialIdfname">
          <el-input
            v-model="form.fmaterialIdfname"
            disabled
            placeholder="请输入物料名称"
          />
        </el-form-item>
        <el-form-item label="采购计量" prop="fconvertDenominator">
          <el-input
            v-model="form.fconvertDenominator"
            placeholder="请输入采购计量"
          />
        </el-form-item>
        <el-form-item label="采购单位名称" prop="fcurrentUnitIdFName">
          <el-input
            v-model="form.fcurrentUnitIdFName"
            @focus="focusOS(1)"
            placeholder="请输入采购单位名称"
          />
        </el-form-item>
        <el-form-item label="采购单位编码" prop="fcurrentUnitIdFNumber">
          <el-input
            v-model="form.fcurrentUnitIdFNumber"
            disabled
            placeholder="请输入采购单位编码"
          />
        </el-form-item>
        <el-form-item label="核算计量" prop="fconvertNumerator">
          <el-input
            v-model="form.fconvertNumerator"
            placeholder="请输入核算计量"
          />
        </el-form-item>
        <el-form-item label="核算计量单位名称" prop="fdestUnitIdfname">
          <el-input
            v-model="form.fdestUnitIdfname"
            @focus="focusOS(2)"
            placeholder="请输入核算计量单位名称"
          />
        </el-form-item>
        <el-form-item label="核算计量单位编码" prop="fdestUnitIdfnumber">
          <el-input
            v-model="form.fdestUnitIdfnumber"
            disabled
            placeholder="请输入核算计量单位编码"
          />
        </el-form-item>

        <el-form-item label="规格型号" prop="fmaterialIdFspecification">
          <el-input
            v-model="form.fmaterialIdFspecification"
            placeholder="请输入规格型号"
          />
        </el-form-item>
        <!-- <el-form-item label="删除标志" prop="del">
          <el-input v-model="form.del" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 选择物料 -->
    <el-dialog
      @open="onMaterialsOpen"
      @close="onMaterialsClose"
      width="40%"
      title="选择物料"
      :visible.sync="materialsVisible"
    >
      <el-form
        :model="materialsQueryParams"
        :inline="true"
        size="small"
        label-width="100px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item prop="">
              <el-input
                v-model="materialsQueryParams.stapCode"
                placeholder="请输入原料编码"
              ></el-input>
            </el-form-item>
            <el-form-item prop="">
              <el-input
                v-model="materialsQueryParams.stapName"
                placeholder="请输入原料名称"
              ></el-input>
            </el-form-item>
            <el-button @click="getMaterialsList">查找</el-button>
          </el-col>
        </el-row>
      </el-form>
      <el-table
        highlight-current-row
        @current-change="materialsChange"
        :data="materialsData"
        style="width: 100%"
      >
        <el-table-column align="center" prop="stapTypeName" label="类别">
        </el-table-column>
        <el-table-column prop="stapCode" align="center" label="原料编码">
        </el-table-column>
        <el-table-column prop="stapName" align="center" label="原料名称">
        </el-table-column>
        <el-table-column align="center" prop="stapBaseUnit" label="单位">
        </el-table-column>
      </el-table>
      <pagination
        :total="materialstotal"
        :page.sync="materialsQueryParams.pageNum"
        :limit.sync="materialsQueryParams.pageSize"
        @pagination="getMaterialsList"
      />
      <el-row style="margin-top: 30px">
        <el-col :span="12">
          <el-button type="primary" size="medium" round @click="MaterialsCan()">
            确定
          </el-button>
        </el-col>
        <el-col :span="12">
          <el-button
            type="primary"
            size="medium"
            round
            @click="MaterialsDeny()"
          >
            取消
          </el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 选择单位 -->
    <el-dialog
      @open="onpen"
      @close="OnClose"
      title="选择单位"
      :visible.sync="UnitVisittion"
      width="40%"
    >
      <el-table
        highlight-current-row
        @current-change="unitChange"
        :data="unitList"
        style="width: 100%"
      >
        <el-table-column prop="id" align="center" label="id"> </el-table-column>
        <el-table-column align="center" prop="fname" label="单位">
        </el-table-column>
        <el-table-column prop="fnumber" align="center" label="单位简写">
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="unitDeny">取 消</el-button>
        <el-button type="primary" @click="unitCan">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="更新数据" :visible.sync="dialogVisible" width="30%">
      <div class="top_Noe">
        <el-form :model="queryparams" size="small" :inline="true">
          <el-form-item prop="stapCode">
            <el-input
              v-model="queryparams.stapCode"
              placeholder="物料编码"
              clearable
            />
          </el-form-item>
          <el-button type="primary" size="mini" @click="UpdateFoode">
            更新
          </el-button>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { bomDish_bom } from "@/api//system/bom.js";
import {
  listUnitTranfer,
  getUnitTranfer,
  delUnitTranfer,
  addUnitTranfer,
  updateUnitTranfer,
  getRemoteStap,
} from "@/api/bom/unitTranfer";
import { getMeasureUnit } from "@/api//system/stap_unit.js";

export default {
  name: "UnitTranfer",
  data() {
    return {
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料单位转化表表格数据
      unitTranferList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        FMaterialIdfnumber: null,
        FMaterialIdfname: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 选择单位
      UnitVisittion: false,
      // 选择物料
      materialsVisible: false,
      tableData: [],
      queryparam: {
        factor: 1,
      },
      stapVisittion: false,
      ShowEditVisition: false,
      materialsQueryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      materialstotal: 0,
      materialsData: [],
      materialsList: {},
      unitList: [],
      unitNav: {},
      unitQuery: {},
      editTitle: false,
      queryparams: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料单位转化表列表 */
    getList() {
      this.loading = true;
      listUnitTranfer(this.queryParams).then((response) => {
        this.unitTranferList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        FMaterialIdfnumber: null,
        FMaterialIdfname: null,
        FConvertDenominator: null,
        FCurrentUnitIdFName: null,
        FCurrentUnitIdFNumber: null,
        FConvertNumerator: null,
        FDestUnitIdfnumber: null,
        FDestUnitIdfname: null,
        FMaterialIdFspecification: null,
        createTime: null,
        del: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料单位转化表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUnitTranfer(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.editTitle = true;
        console.log(this.form);
        this.title = "修改物料单位转化表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateUnitTranfer(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUnitTranfer(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除物料单位转化表编号为"' + ids + '"的数据项？')
        .then(function () {
          return delUnitTranfer(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "bom/unitTranfer/export",
        {
          ...this.queryParams,
        },
        `unitTranfer_${new Date().getTime()}.xlsx`
      );
    },
    unitChange(v) {
      if (v) {
        this.unitNav = v;
      }
    },
    unitCan() {
      if (this.ShowAnd == 1) {
        this.form.fcurrentUnitIdFNumber = this.unitNav.fnumber;
        this.form.fcurrentUnitIdFName = this.unitNav.fname;
      } else {
        this.form.fdestUnitIdfnumber = this.unitNav.fnumber;
        this.form.fdestUnitIdfname = this.unitNav.fname;
      }

      // this.form.auditStatus = 1;
      this.UnitVisittion = false;
    },
    unitDeny() {
      this.unitNav = {};
      this.unitList = [];
      this.UnitVisittion = false;
    },
    // 选择单位弹框打开
    onpen() {
      getMeasureUnit({ ...this.unitQuery }).then(
        (res) => {
          if (res.code == 200) {
            this.unitList = res.data;
          } else {
            this.$message({
              message: res.msg,
              type: "warning",
              center: true,
            });
          }
        },
        (error) => {
          this.$message({
            message: error,
            type: "warning",
            center: true,
          });
        }
      );
    },
    OnClose() {},
    onMaterialsOpen() {
      this.getMaterialsList();
    },
    //物料查询
    getMaterialsList() {
      let TqlDish = {
        ...this.materialsQueryParams,
      };
      bomDish_bom(TqlDish).then(
        (response) => {
          // console.log(response);
          if (response.code == 200) {
            this.materialsData = response.rows;
            this.materialstotal = response.total;
          } else {
            this.$message({
              message: response.msg,
              type: "warning",
              center: true,
            });
          }
        },
        (error) => {
          this.$message({
            message: error,
            type: "warning",
            center: true,
          });
        }
      );
    },
    //物料弹框关闭
    onMaterialsClose() {
      this.materialsQueryParams.stapCode = "";
      this.materialsQueryParams.stapName = "";
      // this.activeName = "first";
    },
    //选中对应的物料
    materialsChange(v) {
      if (v) {
        this.materialsList = v;
      }
    },
    //物料点击确定
    MaterialsCan() {
      // this.form.baseUnit = this.materialsList.stapBaseUnit;
      this.form.fmaterialIdfname = this.materialsList.stapName;
      this.form.fmaterialIdfnumber = this.materialsList.stapCode;
      this.materialsVisible = false;
    },
    // 物料点击取消
    MaterialsDeny() {
      this.materialsVisible = false;
    },
    onMaOpen() {
      this.editTitle = false;
    },
    onMaClose() {},
    UpdateFoode() {
      getRemoteStap({ ...this.queryparams }).then((res) => {
        // console.log();
        // if()
      });
    },

    focusOS(v) {
      this.UnitVisittion = true;
      this.ShowAnd = v;
    },
  },
};
</script>
